﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace PickGold
{
	/// <summary>
	/// 
	/// </summary>
	[ComVisible(true)]
	[Serializable]
	public class EnumeratorStore : IEnumerator
	{
		private IEnumerator _Enumerator;

		/// <summary>
		/// 
		/// </summary>
		public event Action<object> Store;

		/// <summary>
		/// 
		/// </summary>
		public void ClearEvent()
		{
			this.Store = null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="enumerator"></param>
		public EnumeratorStore(IEnumerator enumerator)
		{
			if (enumerator == null)
				throw new ArgumentNullException();

			this._Enumerator = enumerator;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="enumerator"></param>
		/// <param name="store"></param>
		public EnumeratorStore(IEnumerator enumerator, Action<object> store)
		{
			if (enumerator == null)
				throw new ArgumentNullException();

			this._Enumerator = enumerator;
			this.Store += store;
		}

		object IEnumerator.Current
		{
			get
			{
				return this._Enumerator.Current;
			}
		}

		bool IEnumerator.MoveNext()
		{
			var v = this._Enumerator.MoveNext();
			if (v && this.Store != null)
				this.Store(this._Enumerator.Current);
			return v;
		}

		void IEnumerator.Reset()
		{
			this._Enumerator.Reset();
		}
	}

	/// <summary>
	/// 
	/// </summary>
	[ComVisible(true)]
	[Serializable]
	public class EnumeratorStore<T> : IEnumerator<T>
	{
		private IEnumerator<T> _Enumerator;

		/// <summary>
		/// 
		/// </summary>
		public event Action<T> Store;

		/// <summary>
		/// 
		/// </summary>
		public void ClearEvent()
		{
			this.Store = null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="enumerator"></param>
		public EnumeratorStore(IEnumerator<T> enumerator)
		{
			if (enumerator == null)
				throw new ArgumentNullException();

			this._Enumerator = enumerator;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="enumerator"></param>
		/// <param name="store"></param>
		public EnumeratorStore(IEnumerator<T> enumerator, Action<T> store)
		{
			if (enumerator == null)
				throw new ArgumentNullException();

			this._Enumerator = enumerator;
			this.Store += store;
		}

		T IEnumerator<T>.Current
		{
			get
			{
				return this._Enumerator.Current;
			}
		}

		void IDisposable.Dispose()
		{
			this._Enumerator.Dispose();
		}

		object IEnumerator.Current
		{
			get
			{
				return this._Enumerator.Current;
			}
		}

		bool IEnumerator.MoveNext()
		{
			var v = this._Enumerator.MoveNext();
			if (v && this.Store != null)
				this.Store(this._Enumerator.Current);
			return v;
		}

		void IEnumerator.Reset()
		{
			this._Enumerator.Reset();
		}
	}
}
